using Infrastructure.Attribute;
using Infrastructure.Extensions;
using JTZY.Model.System.Dto;
using JTZY.Model.System;
using JTZY.Repository;
using JTZY.Service.System.ISystemService;
using JTZY.Common;

namespace JTZY.Service.System
{
    /// <summary>
    /// 店铺设置表Service业务层处理
    /// </summary>
    [AppService(ServiceType = typeof(ICorecmssettingService), ServiceLifetime = LifeTime.Transient)]
    public class CorecmssettingService : BaseService<Corecmssetting>, ICorecmssettingService
    {
        /// <summary>
        /// 查询店铺设置表列表
        /// </summary>
        /// <param name="parm"></param>
        /// <returns></returns>
        public PagedInfo<CorecmssettingDto> GetList(CorecmssettingQueryDto parm)
        {
            var predicate = QueryExp(parm);

            var response = Queryable()
                .Where(predicate.ToExpression())
                .ToPage<Corecmssetting, CorecmssettingDto>(parm);

            return response;
        }


        /// <summary>
        /// 获取详情
        /// </summary>
        /// <param name="SKey"></param>
        /// <returns></returns>
        public Corecmssetting GetInfo(string SKey)
        {
            var response = Queryable()
                .Where(x => x.SKey == SKey)
                .First();

            return response;
        }

        /// <summary>
        /// 添加店铺设置表
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public Corecmssetting AddCorecmssetting(Corecmssetting model)
        {
            return Insertable(model).ExecuteReturnEntity();
        }

        /// <summary>
        /// 修改店铺设置表
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public int UpdateCorecmssetting(Corecmssetting model)
        {
            return Update(model, true);
        }

        /// <summary>
        /// 获取数据库整合后配置信息
        /// </summary>
        /// <returns></returns>
        public async Task<Dictionary<string, DictionaryKeyValues>> GetConfigDictionaries()
        {
            var configs = SystemSettingDictionary.GetConfig();
            var settings = await GetDatas();
            foreach (KeyValuePair<string, DictionaryKeyValues> kvp in configs)
            {
                var model = settings.Find(p => p.SKey == kvp.Key);
                if (model != null)
                {
                    kvp.Value.sValue = model.SValue;
                }
            }
            return configs;
        }

        /// <summary>
        /// 获取缓存数据
        /// </summary>
        /// <returns></returns>
        private async Task<List<Corecmssetting>> GetDatas()
        {
            //var cache = ManualDataCache.Instance.Get<List<Corecmssetting>>(GlobalConstVars.CacheCoreCmsSettingList);
            //if (cache == null)
            //{
            //    var list = await Queryable().ToListAsync();
            //    ManualDataCache.Instance.Set(GlobalConstVars.CacheCoreCmsSettingList, list, 1440);
            //    return list;
            //}
            //return ManualDataCache.Instance.Get<List<Corecmssetting>>(GlobalConstVars.CacheCoreCmsSettingList);

            return null;
        }

        /// <summary>
        /// 查询导出表达式
        /// </summary>
        /// <param name="parm"></param>
        /// <returns></returns>
        private static Expressionable<Corecmssetting> QueryExp(CorecmssettingQueryDto parm)
        {
            var predicate = Expressionable.Create<Corecmssetting>();

            return predicate;
        }
    }
}